package org.ns.summer.mvc.utils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MvcUtils {
    private final static  String[] ZORE_STR = {};
    public static String[] getPrefixs(String scanPackages) {
        if (scanPackages == null || (scanPackages = scanPackages.trim()).isEmpty()) {
            return ZORE_STR;
        }
        String[] scanPackageArray = scanPackages.split(",");
        List<String> prefixs = new ArrayList<String>(scanPackageArray.length);
        for (String scanPackage : scanPackageArray) {
            scanPackage = scanPackage.trim();
            if (!scanPackage.isEmpty()) {
                scanPackage = scanPackage.replace(".", File.separator);
                prefixs.add(scanPackage);
            }
        }

        return prefixs.toArray(new String[prefixs.size()]);
    }

    /**
     * 合并URL
     * @param urls
     * @return
     */
    public static String margeUrl(String... urls) {
        StringBuilder stringBuilder = new StringBuilder(100);
        for (int i = 0; i < urls.length; i++) {
            String url = urls[i].trim();
            stringBuilder.append(getUrl(url));
        }
        return stringBuilder.toString();
    }

    /**
     * 获取单个URL
     * @param url
     * @return
     */
    public static String getUrl(String url) {
        StringBuilder stringBuilder = new StringBuilder(url.length() + 2);
        int startIndex = 0;
        int endIndex = url.length() - 1;
        for (; startIndex <= endIndex; startIndex++) {
            if (url.charAt(startIndex) != '/') {
                startIndex--;
                break;
            }
        }
        for (; startIndex < endIndex; endIndex --) {
            if (url.charAt(endIndex) != '/') {
                break;
            }
        }
        if (startIndex == -1) {
            stringBuilder.append('/');
            startIndex = 0;
        }
        for (; startIndex <= endIndex; startIndex++) {
            stringBuilder.append(url.charAt(startIndex));
        }
        return stringBuilder.toString();
    }

    public static void out(HttpServletResponse response, String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            response.setStatus(404);
            return;
        }
        try (FileInputStream fin = new FileInputStream(file)) {
            ServletOutputStream out = response.getOutputStream();
            byte[] buffer = new byte[2048];
            int len = 0;
            while ((len = fin.read(buffer)) != -1) {
                out.write(buffer, 0, len);
                out.flush();
            }
            out.close();
        } catch (Exception e) {
            response.setStatus(404);
        }
    }

    public static void main(String[] args) {
//        String[] x1 = getPrefixs("dad,fdsfd,vcx");
//        String[] x2 = getPrefixs("123,324,vcx");
//        System.out.println(Arrays.toString(x1));
//        System.out.println(Arrays.toString(x2));
        System.out.println(getUrl("/adadadas/"));
        System.out.println(getUrl("/"));
        System.out.println(margeUrl("adadada","adsadas","/asd112/"));
    }
}
